import {
    SET_COUPON
} from '../mutation-types';
import api from '../../api';

// import { errFun } from "../../tools.js"

const state = {
    coupons: []

}

const mutations = {
    [SET_COUPON](state, data) {
        state.coupons = data;
        data.forEach(function(el, index) {
            state.coupons[index].created = format(el.created);
        })
    }
}

//格式化日期
function format(date) {
    var date = new Date(date);
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    var h = date.getHours();
    var min = date.getMinutes();
    return y + "年" + m + "月" + d + "日" + h + "时" + min + "分"
}



const actions = {
    GET_COUPON: ({ commit, dispatch }) => {
        //   dispatch('CHANGE_LOADING_SHOW').then(
        api.getUserCoupons().then(response => {
                commit(SET_COUPON, response.data);
                //console.log(response.data);
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            //   )
    }

}

export default {
    state,
    actions,
    mutations
}